#!  python
# -*- coding: UTF-8 -*-
'''
@Author ：Star
@Date ：2022/9/27  
'''
import difflib

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

op = webdriver.ChromeOptions()
op.add_experimental_option("excludeSwitches", ["enable-automation"])
op.add_experimental_option("useAutomationExtension", False)
driver =webdriver.Chrome(options=op)

driver.get("http://news.baidu.com")
sleep(3)
sec = driver.find_element(By.CSS_SELECTOR,"#pane-news > div > ul > li.hdline1 > strong > a:nth-child(1)")
sec_text = sec.text


url = sec.get_attribute("href")
print(url)

sec.click()
sleep(2)

handles= driver.window_handles   #  '-1'代表打开的最后一个窗口
# 切换到新窗口：
driver.switch_to.window(handles[1])

new_title = driver.title
print(driver.title)

nowUrl = driver.current_url
print(nowUrl)

assert url in nowUrl #判断 点击url  和 点击后

sleep(2)
driver.close()


driver.switch_to.window(handles[0])
sleep(2)
driver.find_element(By.CSS_SELECTOR,"#ww").send_keys(sec_text)
sleep(2)
driver.find_element(By.CSS_SELECTOR,"#s_btn_wr").click()
sleep(2)

title = driver.title
content = sec_text

if len(content) > 10:
    content = content[0:10]
if len(title) > 10:
    title = title[7:17]

rate = difflib.SequenceMatcher(None,content,title).quick_ratio()
print("相识度",rate)
print("con",content)
print("ti",title)

assert rate > 0.85  #搜索内容与输入内容一致

driver.quit()


